<?php
if (!defined('FosfoaSystem')){exit(1);}
import($GLOBALS['spConfig']['controller_path'] . '/general.php');
class mail extends general
{
    public function __construct()
    {
        parent::__construct();
        $this->tpl_title = T("Dashboard");
    }
    
    function index()
    {
        $this->tt = "邮件通知模板管理";
        $type=$this->spArgs("type",0);
        $current_page = $this->spArgs("page", 1);
        $page_size = $this->spArgs("size", 30);
        $obj = spClass("email_templateModel");
        if($type==0){
         $conditions = array('1' => 1);   
        }else{
        $conditions = array('type' => $type);
        }
        $arr = $obj->spPager($current_page, $page_size)->findAll($conditions, 'id desc');
        $arrBar = $obj->spPager()->getPager();
        $this->tPageBar = $arrBar;
        $this->arr = $arr;
        $this->type=$type;
        $this->display("admin/mail_index.html");
    }
    function add()
    {
        $type = $this->spArgs("type",0);
        $this->action = 'add';
        $this->type = $type;
        $this->addtitle = "添加邮件通知模板";
        $this->typearr = array(0 => '会员', 1 => '订单', 2 => '发货');
        $this->display("admin/mail_add.html");
    }
    function edit(){
       $this->typearr = array(0 => '会员', 1 => '订单', 2 => '发货');
       $id = $this->spArgs("id", 0);
       $Obj=spClass('email_templateModel');
       $conditions = array("id"=>$id); 
       $mail= $Obj->find($conditions);
       $this->mail = $mail;
       $this->type=$mail['type'];
       $this->addtitle="修改EMAIL通知模板";
       $this->display("admin/mail_add.html");
    }
    function save()
    {
        $strAction = $this->spArgs("action");
        $data = array(
            "type"=>$this->spArgs("type"),
            "title" => $this->spArgs("title"),
            "content" => $this->spArgs("content")
            );
        $Obj = spClass('email_templateModel');    
        if ($strAction == 'add'){
          $Obj->create($data);
          $msg = "邮件模板".$data[title]."添加成功";            
        }else
        {          
            $id =$this->spArgs("id"); 
            $condition = array('id' => $id);
            $Obj->update($condition, $data);
            $msg = "邮件模板".$data[title]."修改成功";     
        }
        Savelog($msg);
        $result = array('status' => 'y',
            'info' => $msg,
            );
        echo json_encode($result); // 返回（显示）JSON结果
    }      
    function del()
    {
        $Obj = spClass("email_templateModel");
        $type = $this->spArgs("type", 0);
        $id2 = $this->spArgs("id", 0);
        if ($id2 == 0)
        {
            $ids = $this->spArgs("selids");
            $idsarray = explode(',', $ids);

            foreach ($idsarray as $id)
            {
                $conditions = array('id' => $id);
                $Obj->delete($conditions); // 删除记录
            }
        } else
        {
            $conditions = array('id' => $id2);
            $Obj->delete($conditions); // 删除记录
        }
        $msg="删除邮件模板".$id2;
        Savelog($msg);
        $this->success(T('Successfully  deleted!'), spUrl("mail", "index",array("type"=>"$type")));
    }
}